CN.record = "条记录";
var swfu;
var msg=CN;
var ufile = {fid:0,filepath:"",name:""};
var userfile = {fid:0,selectfileid:0,name:"",path:"",target:""};
var objectModel = {edit_status:"create",uid:site.uid};
var classmate = {};
$(function(){
	init();
	mpage.modalfactory = function(data,position){//生成列表内容 //TODO 需要变化的地方
        $(".tablist").empty();
        tablist.position = position;//设置数据所在位置
        var tdValue =  "",
            rowclass = "",
            td = function(arg,title){
        		if(!arg)arg="-";
	        	if(title){
	        		return "<td title='"+title+"'>"+arg+"</td>";           
	        	}else{
	        		return "<td>"+arg+"</td>";
	        	}
            },
            setOther = function(arg,type){
            	var showtext = "";
            	switch(type){
	            	case "channel":
	            		switch(arg){
	            		case "WX" :showtext = "微信";break;
	            		case "B2C" :showtext = "网站";break;	            			
	            		}
	            		break;
	            	case "model":
	            		switch(arg){
	            		case "10" :showtext = "默认";break;
	            		case "20" :showtext = "其他";break;
	            		}
	            		break;
            	}
            	return ""+arg+"";
            },
            setStatus = function(arg){
            	var sta = ["未验证","已通过","未通过"];
            	var cla = ["","label-success","label-important"];
            	return "<span class='label "+cla[arg]+"'>"+sta[arg]+"</span>";
            };
            setButton = function(data){
            	if(data.status!=0)
            		return "<span class='label'>已完成验证</span>";
            	var btns = '<div class="btn-group" data-toggle="buttons-radio">';
            	btns += "<a class='btn btn-info btn-mini' onclick='approve(this)' data-name='"+data.name+"' data-id='"+data.baid+"'>验证</a>";
            	btns += "</div>";
            	return btns;
            };
        for ( var element in data) {
        	var kk =data[element],
             	tdValue =
             	td(kk.name)+
             	td(kk.gradename)+
            	td(kk.mobile)+ 
            	td(setOther(kk.channel,"channel"))+
            	td(setOther(kk.model,"model"))+ 
            	td(setStatus(kk.status))+
            	td(timeformat(kk.addtime))+ 
             	td(setButton(kk));
               	rowclass = element%2;//行样式
               $(".tablist").append("<tr class='tr"+rowclass+"'>"+tdValue+"</tr>");
        }
    };
	mpage.init();
}); 

function init(){
	init_dialog();
	init_btn();
}

init_btn = function(){
	$(".cdit").click(function(){//cdit part
		var key = $(this).attr("key"),//条件
			cdit = "status",
			icon = '<span class="caret"></span>',//样式
			title = $(this).text();
		$("#cdtion").val("");
		if(key>-1)
			$("#cdtion").val(cdit+" = "+key);
		$(".cdititle").html(title+icon);
		if(mpage){
			mpage.init();
		}
	});	
		
	$('.choose_cm').change(function(){ 
		var id = $(this).children('option:selected').val();
		if(id==0){
			$("input[type=text]").each(function(index,value){
				var kk = $(this).attr("id");
				if(kk.indexOf("c_")>=0)
					$(this).val("");
			});
		}
		get_classmate(id);
	});
		
	$(".setmore").click(function(){
		var key = $(this).attr("key");
		$(this).attr("key",key == 0 ? 1 : 0);
		$(".moreinfo").toggle();
	});	
}

init_dialog = function(){
	$("#dialog_vali").click(function(){
		$(".dialogmsg").text("");
		var rdata = {name:false,clas:false};
		
		rdata.name = validate_parm("#name","#c_name");
		rdata.clas = validate_parm("#gradename","#c_gradename");
		if(rdata.name && rdata.clas && true){
			$(".dialogmsg").text("验证数据匹配");
		}else{
			$(".dialogmsg").text("验证数据不匹配");
		}
	});
	
	$("#dialog_create").click(function(){//create & open dialog
		objectModel.edit_status = "create";
		open_dialog();
	});
	$(".dialog_save").click(function(){
		var key = $(this).attr("key");
		var text = "是否通过"+objectModel.name+"的验证？确认通过,"+objectModel.name+"将享有校友权限。";
		if(key!=1)
			text = "是否不通过"+objectModel.name+"的验证？确认不通过,"+objectModel.name+"必须重新申请绑定校友的身份。";
		var isnext = confirm(text);		
		if(!isnext)return;
		$(".dialogmsg").text("执行操作中...");
		
		objectModel.uid = site.uid;
		objectModel.url = site.web+site.module+"/approve";
		
		if(!classmate.cid){
			$(".dialogmsg").text("请选择匹配的校友信息进行验证！");
			return;
		}
		objectModel.cid = classmate.cid;
		objectModel.status = key;
		
		complete = function(data){
			done_method = function(){
				$('#myModal').modal('hide');
				mpage.init();
			};
			if(data.code==1){
				$(".dialogmsg").text("操作成功!");
				setTimeout(done_method, 2000);				
			}else{
				alertjson(data);
				$(".dialogmsg").text("系统繁忙，请稍后再试！");
			}
			
		}
		G(objectModel,complete);
	});	
	
	$(".canceldialog").click(function(){//cancel
		clear_dialog();
		$('#myModal').modal('hide');
	});
	$(".canceldialog2").click(function(){//cancel
		$('#myModal2').modal('hide');
		$('#myModal').modal('show');
	});
}

clear_dialog = function(){
	$("#form2 input[type=reset]").click();
	$(".dialogmsg").text("");
}

open_dialog = function(data){
	clear_dialog();
	
	$.each(data, function(index, value) {
		$("#"+index).val(value);
	});
	objectModel = data;
	objectModel.uid = site.uid;
	get_class(objectModel.gid);//init choose_cm		
	$('#myModal').modal('show');
}

validate_parm = function(source,target){
	var result = false;
	$(source).css("color","white");
	$(target).css("color","white");
	if($(source).val()==$(target).val()){
		$(source).css("background","#66CC00");
		$(target).css("background","#66CC00");
		result = true;
	}else{
		$(source).css("background","red");
		$(target).css("background","red");
	}
	return result;
}

get_class = function(gid){
	$(".choose_cm").empty();
	$(".choose_cm").append("<option value='0'>没有匹配的校友信息</option>");
	cb = function(data){
		var option = "";			
		if(data.code==1){
			if(data.data.count!=0){	
				$(".choose_cm").empty();
				$(".choose_cm").append("<option value='0'>请选择校友</option>");
				$.each(data.data.list,function(index,value){
					var cid = data.data.list[index].cid,
						name= data.data.list[index].name;
					option += "<option value='"+cid+"'>"+name+"</option>";
				});
				$(".choose_cm").append(option);
			}					
		}			
		
		$(".cmmsg").hide();
	}
	var json = {url:site.web+"classmate/getgradeclassmate",gid:gid,uid:site.uid};
	G(json,cb)
}

get_classmate = function(id){
	$(".cmmsg").show();
	clear_validate_effect();
	cb = function(data){
		$(".cmmsg").hide();
		if(data.code==1){
			classmate = data.data;
			$.each(classmate,function(index,value){
				$("#c_"+index).val(value);
			});			
		}
	}
	var json = {url:site.web+"classmate/get",cid:id,uid:site.uid};
	G(json,cb)
}

clear_validate_effect = function(){
	$("input[type=text]").each(function(index,value){
		$(this).css("color","black");
		$(this).css("background","#eee");
	})
}

approve = function(arg){
	var id = $(arg).attr("data-id");
	cb = function (data){
		if(data.code==1){
			open_dialog (data.data);
		}else{
			alert("ERROR bind 404");
		}
	}
	var url = site.web+site.module+"/get";
	var json = {url:url,baid:id,uid:site.uid};
	G(json,cb);
}


